package com.tianyi.controller.productInfo;


import com.tianyi.controller.BaseController;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.ProductReq;
import com.tianyi.lib.req.QueryPage;
import com.tianyi.lib.result.ProductInfoOrderResult;
import com.tianyi.model.SimpleLoginUser;
import com.tianyi.productInfo.ProductBaseService;
import com.tianyi.productInfo.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 产品 controller
 *
 * @author cuixuhui
 * @date 2020/06/10
 */
@RestController
@RequestMapping("productInfo")
public class ProductInfoController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ProductInfoController.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductBaseService productBaseService;

    /**
     * 添加产品信息
     * @return
     */
    @RequestMapping(value = "addProductInfo", method = RequestMethod.POST)
    public WebResult<?> addProductInfo(@RequestBody ProductReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        handLoginUser(req);
        return productService.addProductInfo(req);
    }

    /**
     * 全量查询产品信息，只返回三个字段的信息
     * @return
     */
    @RequestMapping(value = "queryProductInfoAllIdNoName", method = RequestMethod.GET)
    public WebResult<?> queryProductInfoAllIdNoName(){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.queryProductInfoAllIdNoName();
    }

    @RequestMapping(value = "addProductInfoNew", method = RequestMethod.POST)
    public WebResult<?> addProductInfoNew(@RequestBody ProductInfoOrderResult req){
        System.out.println(req);
        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.addProductInfoNew(req);
    }

    /**
     * 删除产品信息
     * @return
     */
    @RequestMapping(value = "deleteProductInfoById", method = RequestMethod.GET)
    public WebResult<?> deleteProductInfoById(ProductReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        handLoginUser(req);
        return productService.deleteProductInfoById(req);
    }

    /**
     * 删除产品信息，按照商品货号
     * @return
     */
    @RequestMapping(value = "deleteProductInfoByProductNo", method = RequestMethod.GET)
    public WebResult<?> deleteProductInfoByProductNo(String reqNumber){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.deleteProductInfoByProductNo(reqNumber);
    }

    /**
     * 修改产品信息
     * @return
     */
    @RequestMapping(value = "updateProductInfo", method = RequestMethod.POST)
    public WebResult<?> updateProductInfo(@RequestBody ProductReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        handLoginUser(req);
        return productService.updateProductInfo(req);
    }

    /**
     * 修改产品信息，根据商品货号
     * @return
     */
    @RequestMapping(value = "updateProductInfoNew", method = RequestMethod.POST)
    public WebResult<?> updateProductInfoNew(@RequestBody ProductInfoOrderResult req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.updateProductInfoNew(req);
    }

    /**
     * 产品信息列表
     * @return
     */
    //@GetMapping(value = "queryProductInfoPage")
    //public WebResult<?> queryProductInfoPage(ProductReq req){

        //SimpleLoginUser loginUser = getUserFromRequest();
        //if (Objects.isNull(loginUser)){
         //   return WebResult.error("用户未登陆");
        //}

        //return productService.queryProductInfoPage(req);
       // return WebResult.ok(productBaseService.queryProductInfoPage(req));
    //}

    /**
     * 获取产品详情
     * @return
     */
    @GetMapping(value = "queryProductInfoDetail")
    public WebResult<?> queryProductInfoDetail(ProductReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        handLoginUser(req);
        return productService.queryProductInfoDetail(req);
    }

    @RequestMapping(value = "queryProductInfoPage", method = RequestMethod.POST)
    public WebResult<?> queryProductInfoByPageJSON(@RequestBody QueryPage req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        return productService.queryProductInfoByPageJSON(req);
    }

    /**
     * 获取群组产品详情，通过商品货号
     * @return
     */
    @GetMapping(value = "queryProductInfoByProductNo")
    public WebResult<?> queryProductInfoByProductNo(String productNo){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.queryProductInfoByProductNo(productNo);
    }

    @RequestMapping(value = "addProductInfoNewJson", method = RequestMethod.POST)
    public WebResult<?> addProductInfoNewJson(@RequestBody ProductInfoOrderResult req){
        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.addProductInfoNewJson(req);
    }

    @RequestMapping(value = "updateProductInfoNewJsonById", method = RequestMethod.POST)
    public WebResult<?> updateProductInfoNewJsonById(@RequestBody ProductInfoOrderResult req){
        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.updateProductInfoNewJsonById(req);
    }

    @RequestMapping(value = "deleteProductInfoNewJsonById", method = RequestMethod.GET)
    public WebResult<?> deleteProductInfoNewJsonById(long id){
        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //handLoginUser(req);
        return productService.deleteProductInfoNewJsonById(id);
    }

}
